package com.lancoo.ccas53.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lancoo.ccas53.entity.PeriodScheme;
import com.lancoo.ccas53.mapper.PeriodSchemeMapper;
import com.lancoo.ccas53.pojo.response.basicPlatform.BasePeriodScheme;
import com.lancoo.ccas53.service.PeriodSchemeService;
import com.lancoo.ccas53.service.YunBasicService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课时方案，每种方案分别对应不同的节次时间 服务实现类
 * </p>
 *
 * @author quin.song
 * @since 2024-05-20
 */
@Service
public class PeriodSchemeServiceImpl extends ServiceImpl<PeriodSchemeMapper, PeriodScheme> implements PeriodSchemeService {

    @Resource
    @Lazy
    private YunBasicService yunBasicService;

    @Override
    @Transactional
    public void syncPeriodScheme(Integer taskId, String schoolId) {

        List<BasePeriodScheme> basePeriodScheme = yunBasicService.getPeriodScheme(schoolId);
        if (CollUtil.isEmpty(basePeriodScheme)) {
            return;
        }
        List<PeriodScheme> periodSchemes = new ArrayList<>();
        for (BasePeriodScheme periodScheme : basePeriodScheme) {
            periodSchemes.add(periodScheme.toPeriodScheme(taskId));
        }
//        for (PeriodScheme periodScheme : periodSchemes) {
//            int name = Integer.parseInt(periodScheme.getPeriodName().substring(1, 2));
//            periodScheme.setPeriodName("第" + TimeCodeUtil.intToChineseNum(name) + "节");
//
//        }
        Long periodSchemeCount = this.lambdaQuery().eq(PeriodScheme::getTaskId, taskId).count();
        //数据库和获取的条数不一致  就删掉重新同步
        if (periodSchemeCount != periodSchemes.size()) {
            this.lambdaUpdate().eq(PeriodScheme::getTaskId, taskId).remove();
            this.saveBatch(periodSchemes);
        }
    }
}
